package cn.shangjia.shop.controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.shangjia.common.domain.NewsComment;
import cn.shangjia.common.domain.UserContactOnline;
import cn.shangjia.common.domain.UserInfo;
import cn.shangjia.common.domain.validate.ValidateUtils;
import cn.shangjia.common.util.StringUtils;
import cn.shangjia.common.webservice.bean.NormalMessage;
import cn.shangjia.shop.config.Constants;
import cn.shangjia.shop.controller.base.ShopInitController;


/**
 * 导航页面
 * @author <a href="mailto:liangchuan@knet.cn">liangchuan</a>
 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
 * @author <a href="mailto:wangxin@knet.cn">wangxin</a>
 *
 */
@Controller
@RequestMapping(ShopController.CONTEXT_PATH)
public class ShopController extends ShopInitController{
	
	protected static final String CONTEXT_PATH = "/shop/{username}/";
	
	protected static final String GAO_LIANG = "navHot";
	
	/**
	 *  首页
	 * 
	 * @param map
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	@RequestMapping(value = "index.html", method = {RequestMethod.GET , RequestMethod.HEAD})
	public String index(ModelMap map) {
//		return "shop/templet/" + map.get(Constants.TEMPLATE) + "/index";
		map.addAttribute(GAO_LIANG, "index");
		return "shop/templet/index";
	}

	/**
	 *  供应
	 * 
	 * @param map
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	@RequestMapping(value = "supply.html", method = {RequestMethod.GET , RequestMethod.HEAD})
	public String sale(ModelMap map) {
//		return "shop/templet/" + map.get(Constants.TEMPLATE) + "/sale";
		map.addAttribute(GAO_LIANG, "sale");
		return "shop/templet/sale";
	}
	
	/**
	 *  供应终端页
	 * 
	 * @param map
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	@RequestMapping(value = "supply/{id}.html", method = {RequestMethod.GET , RequestMethod.HEAD})
	public String sale_detail(@PathVariable("id")String id, ModelMap map) {
		if (StringUtils.isEmpty(id))
			return sale(map);
		map.addAttribute("id", id);
//		return "shop/templet/" + map.get(Constants.TEMPLATE) + "/saleDetail";
		map.addAttribute(GAO_LIANG, "sale");
		return "shop/templet/saleDetail";
	}
	
	
	/**
	 * 求购
	 * 
	 * @param map
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	@RequestMapping(value = "buy.html", method = {RequestMethod.GET , RequestMethod.HEAD})
	public String buy(ModelMap map) {
//		return "shop/templet/"+ map.get(Constants.TEMPLATE) +"/buy";
		map.addAttribute(GAO_LIANG, "buy");
		return "shop/templet/buy";
	}	
	
	/**
	 *  供应终端页
	 * 
	 * @param id
	 * @param map
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	@RequestMapping(value = "buy/{id}.html", method = {RequestMethod.GET , RequestMethod.HEAD})
	public String buy_detail(@PathVariable("id")String id, ModelMap map) {
		if (StringUtils.isEmpty(id))
			return buy(map);
		map.addAttribute("id", id);
//		return "shop/templet/" + map.get(Constants.TEMPLATE) + "/buyDetail";
		map.addAttribute(GAO_LIANG, "buy");
		return "shop/templet/buyDetail";
	}
	
	
	/**
	 *  企业介绍
	 * 
	 * @param map
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	@RequestMapping(value = "intro.html", method = {RequestMethod.GET , RequestMethod.HEAD})
	public String companyProfile(ModelMap map) {
//		return "shop/templet/"+ map.get(Constants.TEMPLATE) +"/companyProfile";
		map.addAttribute(GAO_LIANG, "companyProfile");
		return "shop/templet/companyProfile";
	}
	
	/**
	 *  企业动态
	 * 
	 * @param map
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	@RequestMapping(value = "news.html", method = {RequestMethod.GET , RequestMethod.HEAD})
	public String companyNews(ModelMap map) {
//		return "shop/templet/"+ map.get(Constants.TEMPLATE) +"/companyNews";
		map.addAttribute(GAO_LIANG, "companyNews");
		return "shop/templet/companyNews";
	}
	
	/**
	 *  企业动态终端页
	 * 
	 * @param map
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	@RequestMapping(value = "news/{id}.html", method = {RequestMethod.GET , RequestMethod.HEAD})
	public String companyNewsDetail(@PathVariable("id")String id, ModelMap map) {
		if (StringUtils.isEmpty(id))
			return companyNews(map);
		map.addAttribute("id", id);
//		return "shop/templet/" + map.get(Constants.TEMPLATE) + "/companyNewsDetail";
		map.addAttribute(GAO_LIANG, "companyNews");
		return "shop/templet/companyNewsDetail";
	}
	
	
	/**
	 *  荣誉证书
	 * 
	 * @param map
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	@RequestMapping(value = "honor.html", method = {RequestMethod.GET , RequestMethod.HEAD})
	public String companyHonor(ModelMap map) {
//		return "shop/templet/"+ map.get(Constants.TEMPLATE) +"/companyHonor";
		map.addAttribute(GAO_LIANG, "companyHonor");
		return "shop/templet/companyHonor";
	}	
	
	/**
	 * 
	 * 商铺视频.
	 *
	 * @param map
	 * @return
	 * @author <a href="mailto:wangxin@knet.cn">北京王欣</a>
	 */
	@RequestMapping(value = "video.html", method = {RequestMethod.GET , RequestMethod.HEAD})
	public String video(ModelMap map) {
//		return "shop/templet/"+ map.get(Constants.TEMPLATE) +"/video";
		map.addAttribute(GAO_LIANG, "video");
		return "shop/templet/video";
	}
	
	/**
	 * 
	 * 商铺视频终端页.
	 *
	 * @param id
	 * @param map
	 * @return
	 * @author <a href="mailto:wangxin@knet.cn">北京王欣</a>
	 */
	@RequestMapping(value = "video/{id}.html", method = {RequestMethod.GET , RequestMethod.HEAD})
	public String videoDetail(@PathVariable("id")String id, ModelMap map) {
		if (StringUtils.isEmpty(id))
			return video(map);
		map.addAttribute("id", id);
//		return "shop/templet/" + map.get(Constants.TEMPLATE) + "/videoDetail";
		map.addAttribute(GAO_LIANG, "video");
		return "shop/templet/videoDetail";
	}
	
	/**
	 *  联系我们
	 * 
	 * @param map
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	@RequestMapping(value = "contact.html", method = {RequestMethod.GET , RequestMethod.HEAD})
	public String contant(ModelMap map) {
//		return "shop/templet/"+ map.get(Constants.TEMPLATE) +"/contact";
		map.addAttribute(GAO_LIANG, "contact");
		return "shop/templet/contact";
	}	
	
	/**
	 *  在线留言
	 * 
	 * @param map
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	@RequestMapping(value = "message.html", method = {RequestMethod.GET , RequestMethod.HEAD})
	public String message(ModelMap map) {
//		return "shop/templet/"+ map.get(Constants.TEMPLATE) +"/message";
		map.addAttribute(GAO_LIANG, "message");
		return "shop/templet/message";
	}
	
	//-------------------------------------submit--------------------------------------------------------
	
	/**
	 *  提交评论
	 * 
	 * @param id
	 * @param map
	 * @param comment
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	@RequestMapping(value = "news/{id}.html", method = {RequestMethod.POST})
	public String companyNewsComment(@PathVariable("id")String id, ModelMap map, NewsComment comment) {
		if (StringUtils.isEmpty(id)) {
			id = comment.getNewsId();
			if (StringUtils.isEmpty(id))
				return companyNews(map);
		}
		Map<String, String> errors = ValidateUtils.validate(comment);
		if (!errors.isEmpty()) {
			map.addAttribute("errors", errors);
			return companyNewsDetail(id, map);
		}
		newsService.addComment(comment);
		String username = (String) map.get("username");
		map.clear();
		return "redirect:/shop/" + username + "/news/" + id + ".html";
	}
	
	/**
	 *  提交留言
	 * 
	 * @param map
	 * @param title
	 * @param content
	 * @param valCode
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	@RequestMapping(value = "message.html", method = {RequestMethod.POST})
	public String message_submit(ModelMap map, String title, String content, String valCode) {
		
		String toUserId = (String) map.get("userid");
		UserInfo currentUserInfo = (UserInfo) map.get(Constants.CURRENT_USERINFO);
		
		if (currentUserInfo == null) {
			map.addAttribute(Constants.ERRORS, "您还没有登录, 请登录后发送消息");
			return message(map);
		}
		if (StringUtils.isEmpty(title)) {
			map.addAttribute(Constants.ERRORS, "标题不能为空!");
			return message(map);
		}
		if (title.getBytes().length > 64) {
			map.addAttribute(Constants.ERRORS, "标题长度不能大于64个字符!");
			return message(map);
		}
		if (StringUtils.isEmpty(content)) {
			map.addAttribute(Constants.ERRORS, "内容不能为空!");
			return message(map);
		}
		if(content.getBytes().length > 512) {
			map.addAttribute(Constants.ERRORS, "内容长度不能大于512个字符!");
			return message(map);
		}
		
		String sendUserId = currentUserInfo.getUser().getId();
		String sendNickName = currentUserInfo.getUserContact().getNickName();
		NormalMessage message = new NormalMessage();
		message.setSenderId(sendUserId);
		message.setSenderNickName(sendNickName);
		message.setReceiverId(toUserId);
		UserContactOnline userContact = (UserContactOnline) map.get(Constants.USER_CONTACT);
		String toNickName = userContact.getNickName();
		message.setReceiverNickName(toNickName);
		message.setTitle(title);
		message.setContent(content);
		messageService.addNormalMessage(message);
		
		String username = (String) map.get("username");
		map.clear();
		return "redirect:/shop/" + username + "/message.html?submit=success";
	}
	
}
